package com.gzgy.ustore.api.service.impl;

import com.gzgy.ustore.api.common.exception.BaseAPIException;
import com.gzgy.ustore.api.common.exception.ExceptionCodeStatusEnum;
import com.gzgy.ustore.api.common.util.DateUtil;
import com.gzgy.ustore.api.common.util.I18nUtil;
import com.gzgy.ustore.api.dao.DvcGroupDao;
import com.gzgy.ustore.api.model.DvcGroupList;
import com.gzgy.ustore.api.model.DvcGroupListTree;
import com.gzgy.ustore.api.service.DvcGroupService;
import com.ustore.model.DvcGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.List;

@Service
public class DvcGroupServiceImpl implements DvcGroupService {

    @Autowired
    private DvcGroupDao dvcGroupDao;

    @Override
    public DvcGroupListTree dvcGroupTree(Integer companyId) {
        //top tree
        DvcGroupListTree topTree = new DvcGroupListTree();
        topTree.setDvcGroupCode(0);
        topTree.setName("全部");
        List<DvcGroupListTree> childrenLists = dvcGroupDao.dvcGroupListTree(companyId);
        topTree.setChildren(childrenLists);
        topTree.setOpen(true);
        return topTree;
    }

    @Override
    public List<DvcGroupList> dvcGroupList(Integer companyId) {

        return dvcGroupDao.dvcGroupList(companyId);
    }

    @Override
    public String dvcGroupOperation(Integer dvcGroupId, Integer companyId, String operationFlag, String dvcGroupName, String remark, String userId) throws SQLException {
        DvcGroup dvcGroup = null;
        int dvcNotFoundErrorCode = ExceptionCodeStatusEnum.DVC_GROUP_NOT_FOUND_EXCEPTION.toInt();
        int unkownParamterErrorCode = ExceptionCodeStatusEnum.UNKOWN_PARAMTER_EXCEPTION.toInt();
        //新增设备组操作
        if (DvcGroup.DVC_GROUP_OPERATION_ADD.equals(operationFlag)) {
            dvcGroup = new DvcGroup();
            dvcGroup.setCompanyId(companyId);
            dvcGroup.setDvcGroupName(dvcGroupName);
            dvcGroup.setRemark(remark);
            dvcGroup.setCreatedBy(userId);
            dvcGroup.setCreatedTime(DateUtil.getCurrentDate());
            dvcGroupDao.add(dvcGroup);
            return dvcGroup.getId().toString();
            //更新设备组操作
        } else if (DvcGroup.DVC_GROUP_OPERATION_UPDATE.equals(operationFlag)) {
            dvcGroup = dvcGroupDao.get(dvcGroupId);
            //找不到设备组，出错
            if (dvcGroup == null) {
                throw new BaseAPIException(I18nUtil.getMessage(dvcNotFoundErrorCode, null, "zh_CN"), dvcNotFoundErrorCode);
            }
            dvcGroup.setRemark(remark);
            dvcGroup.setDvcGroupName(dvcGroupName);
            dvcGroup.setUpdatedTime(DateUtil.getCurrentDate());
            dvcGroup.setUpdatedBy(userId);
            Boolean result = dvcGroupDao.update(dvcGroup);
            if (!result) {
                int errorCode = ExceptionCodeStatusEnum.DVC_GROUP_UPDATE_FAIL_EXCEPTION.toInt();
                throw new BaseAPIException(I18nUtil.getMessage(errorCode, null, "zh_CN"), errorCode);
            }
            return "设备组更新成功";
            //设备删除操作
        } else if (DvcGroup.DVC_GROUP_OPERATION_DELETE.equals(operationFlag)) {
            dvcGroup = dvcGroupDao.get(dvcGroupId);
            //找不到设备组，出错
            if (dvcGroup == null) {
                throw new BaseAPIException(I18nUtil.getMessage(dvcNotFoundErrorCode, null, "zh_CN"), dvcNotFoundErrorCode);
            }
            Boolean result = dvcGroupDao.del(dvcGroupId);
            if (!result) {
                int errorCode = ExceptionCodeStatusEnum.DVC_GROUP_DELETE_FAIL_EXCEPTION.toInt();
                throw new BaseAPIException(I18nUtil.getMessage(errorCode, null, "zh_CN"), errorCode);
            }
            return "设备组删除成功";
        }
        throw new BaseAPIException(I18nUtil.getMessage(unkownParamterErrorCode, null, "zh_CN"), unkownParamterErrorCode);
    }
}
